/**
 * 
 */
package com.mobile2you.nci.android;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.text.util.Linkify;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.maps.MapActivity;
import com.mobile2you.nci.android.domain.Centres;
import com.mobile2you.nci.android.utils.LoadImagePool;
import com.mobile2you.nci.android.utils.Utils;

/**
 * @author JBromo
 * 
 */
public class MenuCentreDetail extends MapActivity {

	ImageView imageView;

	TextView textCentre;

	View waitingImage;

	TextView titleCentres;

	private void initViews() {

		imageView = (ImageView) findViewById(R.id.img_centre_detail);
		waitingImage = findViewById(R.id.progress_waiting_image);
		textCentre = (TextView) findViewById(R.id.label_centre_detail);
		View directionMode = findViewById(R.id.view_centre_direction);
		directionMode.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Utils.lancerActiviter(MenuCentreDetail.this,
						MenuCentreMapDetail.class);

			}
		});
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.menu_centre_detail);
		titleCentres = (TextView) findViewById(R.id.label_title_centre);
		initViews();
		showDetailview(Utils.centreSelected);

	}

	private void showDetailview(final Centres centres) {
		titleCentres.setText(centres.getTitle());
		Utils.centreSelected = centres;
		imageView.setVisibility(View.GONE);
		waitingImage.setVisibility(View.VISIBLE);
		textCentre.setText(centres.getTexte());
		if (centres.getBitmap() == null) {
			LoadImagePool.loadImage(this, centres.getImageURL(), new Handler(
					new Callback() {
						@Override
						public boolean handleMessage(Message msg) {
							waitingImage.setVisibility(View.GONE);
							imageView.setVisibility(View.VISIBLE);
							if (msg.obj != null) {
								Bitmap image = (Bitmap) msg.obj;

								imageView
										.setBackgroundDrawable(new BitmapDrawable(
												MenuCentreDetail.this
														.getResources(), image));
								centres.setBitmap(image);

							} else {
								imageView
										.setBackgroundResource(R.drawable.no_image_nci);
							}
							return false;
						}
					}), true);
		} else {
			imageView.setBackgroundDrawable(new BitmapDrawable(
					MenuCentreDetail.this.getResources(), centres.getBitmap()));
		}

		TextView head1 = (TextView) findViewById(R.id.label_head_1);
		head1.setText(centres.getHeading1());

		TextView head1Value = (TextView) findViewById(R.id.label_head_1_value);
		head1Value.setText(centres.getTexte1());

		TextView head2 = (TextView) findViewById(R.id.label_head_2);
		head2.setText(centres.getHeading2());

		TextView head2Value = (TextView) findViewById(R.id.label_head_2_value);
		head2Value.setText(centres.getTexte2());
		Linkify.addLinks(head2Value, Linkify.EMAIL_ADDRESSES | Linkify.ALL);

	}

}
